package Connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


/**
 *
 * @author william
 */
public class EchoServer 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {
        ServerSocket serverSocket = null;
        
        try
        {
            serverSocket = new ServerSocket(33333);
        }
        catch(IOException ioe)
        {
            System.err.println("Could not listen on port 33333");
            System.exit(1);
            
        }
        Socket clientSocket = null;
        try
        {
            System.err.println("Waiting for client to connect...");
            clientSocket = serverSocket.accept();
        }
        catch(IOException ioe)
        {
            System.err.println("Clinet accept failed.....");
            System.exit(1);
        }
        System.out.println("Client accepted at " + clientSocket.getRemoteSocketAddress());
        
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        
        String inputLine;
        String outputLine;
        
        while((inputLine = in.readLine()) != null)
        {
            System.out.println("The Client says: " + inputLine);
            out.println(inputLine);
            if(inputLine.equalsIgnoreCase("BYE"));
            {
                break;
            }            
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
        
        System.out.println("DONE");
    }
}
